﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    /// <summary>
    /// RelatedSemiReportHandler 的摘要说明
    /// </summary>
    public class RelatedSemiReportHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string reportname = context.Request.QueryString["reportname"] != null ? context.Request.QueryString["reportname"].ToString() : "";

            if (reportname == "standardreport")
            {

                HandlerStandardReport standardReport = new HandlerStandardReport();
                standardReport.ProcessRelatedData(context);
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        /// <summary>
        /// 标准报表数据接口
        /// </summary>
        /// <param name="begindate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="sendpoints">发报时刻数组</param>
        /// <param name="wthspans">天气状态时间段</param>
        /// <param name="tempspans">温度状态时间段</param>
        /// <param name="tablename">数据表类型</param>
        /// <param name="tid">执行任何号</param>
        /// <returns></returns>
        public string GetStandardReportData(string begindate, string enddate, string sendpoints, string wthspans, string tempspans, string memberTable, string standardTable, string stations, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            ForeSemiType semiType = ForeSemiType.Auto;
            //DataTableType tableName = (DataTableType)Enum.Parse(typeof(DataTableType), tablename);

            List<ForeSendpointType> sendpointList = new List<ForeSendpointType>();
            foreach (string s in sendpoints.Split(','))
                sendpointList.Add((ForeSendpointType)Enum.Parse(typeof(ForeSendpointType), s.Replace("'", "")));
            ForeSendpointType[] sendpointArray = sendpointList.ToArray();

            List<ForeSpanType> spanList = new List<ForeSpanType>();
            foreach (string s in wthspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] wthSpanArray = spanList.ToArray();

            spanList.Clear();
            foreach (string s in tempspans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] tempSpanArray = spanList.ToArray();

            ForeSendpointType[] sendPoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

            StandardReport standardReport = new StandardReport(connectionString, memberTable,standardTable, stations, tid, true);
            standardReport.PrepareData(beginDate, endDate, semiType, wthSpanArray, tempSpanArray, sendpointArray);
            return standardReport.GetReportDataString();

        }
    }
}